<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="设备编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="eqmCode">
              <a-input v-model="model.eqmCode" placeholder="请输入设备编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="eqmName">
              <a-input v-model="model.eqmName" placeholder="请输入设备名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="设备型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="eqmModel">
              <a-input v-model="model.eqmModel" placeholder="请输入设备型号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="设备品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="eqmBrand">
              <a-input v-model="model.eqmBrand" placeholder="请输入设备品牌"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="设备重量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="eqmWeight">
              <a-input-number v-model="model.eqmWeight" placeholder="请输入设备重量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="使用年限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceYear">
              <a-input-number v-model="model.serviceYear" placeholder="请输入使用年限" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工作中心" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workCenterId">
              <j-tree-select
                v-model="model.workCenterId"
                dict="mdm_work_center,name,id"
                pid-field="pid">
              </j-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="产线" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productLineId">
              <j-dict-select-tag type="list" v-model="model.productLineId" dictCode="mdm_product_line,name,id" placeholder="请选择产线" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="放置地点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="placeDesc">
              <a-input v-model="model.placeDesc" placeholder="请输入放置地点"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplyId">
              <j-dict-select-tag type="list" v-model="model.supplyId" dictCode="mdm_supply,supply_full_name,id" placeholder="请选择供应商" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="制造商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manufacturer">
              <a-input v-model="model.manufacturer" placeholder="请输入制造商"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="安装日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="setDate">
              <j-date placeholder="请选择安装日期" v-model="model.setDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="出厂日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productionDate">
              <j-date placeholder="请选择出厂日期" v-model="model.productionDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="启用日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startDate">
              <j-date placeholder="请选择启用日期" v-model="model.startDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="iem_device_state" placeholder="请选择设备状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="manager">
              <j-select-user-by-dep v-model="model.manager" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="使用部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useDept">
              <j-select-depart v-model="model.useDept" multi  />
            </a-form-model-item>
          </a-col>
          <a-col :span="16">
            <a-form-model-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark">
              <a-textarea v-model="model.remark" rows="3" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MdmEquipmentForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      model:{
        status:'10'
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      validatorRules: {
        eqmCode: [
          { required: true, message: '请输入设备编号!'},
          { validator: (rule, value, callback) => validateDuplicateValue('mdm_equipment', 'eqm_code', value, this.model.id, callback)},
        ],
        eqmName: [
          { required: true, message: '请输入设备名称!'},
        ],
        status: [
          { required: true, message: '请输入设备状态!'},
        ],
      },
      url: {
        add: "/mdm/equipment/add",
        edit: "/mdm/equipment/edit",
        queryById: "/mdm/equipment/queryById"
      }
    }
  },
  computed: {
    formDisabled(){
      return this.disabled
    },
  },
  created () {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit (record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          httpAction(httpurl,this.model,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>